import auxMain as am
import pandas as pd

'''
This is the main file to reproduce:
    Figure 3 panel d
    Figure 4 panel c
    Table 1
    All the robustness tests tables in the Supplementary Information
    
Running the code requires access to:
    1. Compustat Annual Fundamentals North America and Global
    2. Trucost Paris Alignment data
    3. Trucost environmental data
    4. Refinitiv Asset4
    5. The behavioural dataset provided with the paper in the local_data/ folder

'''

#===============
#===============
AM = am.main_results()

#%% Load dataset
ReducedData = pd.read_csv('local_data/dataset_.csv')

#%% Isolate companies in the hard to abate and energy-intensive sectors
SECTORSTOINCLUDE = ['Industrial', 'Material', 'Energy', 'Utilities']
ReducedData = ReducedData[ReducedData.GICS_level_0.isin(SECTORSTOINCLUDE)]

#%% Remove companies with less than two years of observations
idx = ReducedData[['rfyear', 'gvkey']].groupby('gvkey').count()
idx = idx[idx > 1].dropna().index
ReducedData = ReducedData[ReducedData.gvkey.isin(idx)]
ReducedData['em_intensity'] = ReducedData['DirectControl']/ReducedData['sale_usd']


#=============================================================================
#== Estimate the model with the total number of initiatives: Figure 3 panel d
#=============================================================================
#%% First figure
target_type = 'below'
size_quartile = -1
estimation_year = 2019
AM.table_total(ReducedData, size_quartile, target_type, estimation_year, causative_only_actions=False)


#=====================================================
#== Main estimation: Figure 4 panel c
#=====================================================
#%% Main text Figure
target_type = 'below'
size_quartile = 3
estimation_year = 2019
AM.table_differential_behaviour(ReducedData, size_quartile, target_type, estimation_year, causative_only_actions=False)

    
#=====================================================
#== Estimate the model by size quartile
#=====================================================   
#%% By Size quartile
only_causative_actions = False
target_type = 'below'
estimation_year = 2019
AM.table_by_size(ReducedData,  target_type, estimation_year, causative_only_actions=only_causative_actions)


#=====================================================
#== Estimate the model by year of alignment estimation
#=====================================================
#%% By Estimation year
only_causative_actions = False
target_type = 'below'
AM.table_by_time(ReducedData,  target_type, estimation_year, causative_only_actions=only_causative_actions)

#===============================================
#== Estimate the model without SDG dimensions
#===============================================
#%% Estimation of the model without SDG in the construction of the differential behaviour
target_type = 'below'
size_quartile = 3
estimation_year = 2019
AM.table_with_no_SDGs(ReducedData, size_quartile, target_type, estimation_year)


#==================================================
#== Estimate the model without activity dimensions
#==================================================
#%% Estimation of the model without ACT in the construction of the differential behaviour
target_type = 'below'
size_quartile = 3
estimation_year = 2019
AM.table_with_no_actions(ReducedData, size_quartile, target_type, estimation_year)


